package org.testis.testdriver.operations;

import org.testis.testdriver.AtofEnumaration.DebuggingMode;
import org.testis.testdriver.StepContainer;
import org.testis.testdriver.TestPart;
import org.testis.testdriver.TestStep;
import org.testis.testdriver.resultsmatrix.ThreeDResultsMatrix;


/**
 *
 * @author barisergun
 */
public class TestPartOperationSetThreeDResult extends TestPartOperationDecorator
{

    private TestPart partInUse;


    public TestPartOperationSetThreeDResult(TestPart testPart)
    {
        partInUse = testPart;
    }


    @Override
    public boolean runOperation(DebuggingMode mode) throws InterruptedException
    {
        boolean result = super.runOperation(mode);
        runSpecificBehaviour(result);
        return result;

    }


    /**
     * This method is used to drive the specific Tasks that belong to this decorator
     * operation.
     *
     * @return boolean the result of operation. This result will affect the operation
     * of linked decorator tasks.
     */
    private void runSpecificBehaviour(boolean result)
    {
        ThreeDResultsMatrix ourMatrix = ((StepContainer) partInUse.getParent()).
            getResultMatrix();
        if (ourMatrix != null) {
            int invocationCountIndex = ((StepContainer) partInUse.getParent()).
                getInvocationCountIndex();
            int childIndex = 0;
            int childDataIndex = 0;

            if (partInUse instanceof TestStep) {
                // If the Step is not DataArrayAppliesToCase its result must be set
                // during Test Step result validation. To the related TestCaseDataındex.
                if (((TestStep) partInUse).getDataArrayAppliesToCase()) {
                    childIndex = partInUse.getParent().getTestPartList().indexOf(
                        partInUse);
                    childDataIndex = ((StepContainer) partInUse.getParent()).
                        getTestCaseDataIndex() ;// Case Data Index starts from 1

                    ourMatrix.setResultIn3dArray(invocationCountIndex,
                                                 childIndex,
                                                 childDataIndex,
                                                 result);
                }


            }
            else if (partInUse instanceof StepContainer) // The only StepContainer is the TestStepGroup
            {
                StepContainer containerInUse = (StepContainer) partInUse;
                childIndex = partInUse.getParent().getTestPartList().
                    indexOf(
                    partInUse);
                childDataIndex = ((StepContainer) partInUse.getParent()).
                    getTestCaseDataIndex();// Case Data Index starts from 1

                ourMatrix.setResultIn3dArray(invocationCountIndex,
                                             childIndex,
                                             childDataIndex,
                                             result);

            }


        }


    }
}
